import redis
import json


class Chat:
    def __init__(self):
        self.redis = redis.from_url(url="redis://localhost:6379/0")

    def chat_log(self, uid, page, size):
        messages = self.redis.lrange("message_store:"+uid, (page-1)*size, page*size-1)
        result = []
        for message in messages:
            result.append(json.loads(message))
        return result

    # 清空聊天记录
    def clear_chat_log(self, uid):
        self.redis.delete("message_store:"+uid)
